package com.yulinlin.admin.modules.generate.service;

import com.yulinlin.admin.common.util.TemplateUtil;
import com.yulinlin.admin.modules.generate.domain.ColumnInfo;
import com.yulinlin.admin.modules.generate.domain.TableInfo;
import com.yulinlin.admin.modules.generate.domain.ViewEnum;
import com.yulinlin.admin.modules.generate.domain.po.GeneratePo;
import com.yulinlin.jdbc.session.SqlSession;
import com.yulinlin.lang.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;


@Component
public class GenerateService {


    @Autowired
    SqlSession sqlSession;


    public  String tableSchema = "php_admin";


    public List<TableInfo> all(){
        TableInfo table = new TableInfo();
        table.setTableSchema(tableSchema);
        return table.createSelectWrapper().selectList();
    }

    private TableInfo findTable(String tableName){
        TableInfo table = new TableInfo();
        table.setTableSchema(tableSchema);
        table.setTableName(tableName);
        return table.createSelectWrapper().selectOne();

    }


    private void genFile(GeneratePo po,ViewEnum view, TableInfo table) throws Exception{


        String requestMapping=   po.getModule().split("\\.")[1]+"/"+ StringUtil.columnToJava(table.getTableName());

        String className =table.getClassName();
        HashMap map =  new HashMap<>();

        map.put("domain",view.getDomain());
        map.put("fileType",view.getFileType());

        map.put("prefix",view.getPrefix());
        map.put("suffix",view.getSuffix());
        map.put("tableComment",table.getTableComment());

        map.put("tableName",table.getTableName());
        map.put("className",className);

        map.put("requestMapping",requestMapping);

        map.put("packageSpace",po.getPackageSpace());
        map.put("moduleName",po.getModule());

        map.put("columnList",table.getColumnList());

        String packagePath =  view.getDomain().replace(".","/");

        String html = TemplateUtil.renderTemplate("/template/"+view.name()+".ftl",map);

        String path = po.getWritePath()+"/"+po.getModule().replace(".","/")+"/"+packagePath+"/"+view.getPrefix()+className+view.getSuffix()+"."+view.getFileType();


        File f  =new File(path);
        f.getParentFile().mkdirs();
        FileOutputStream out=  new FileOutputStream(f);
        out.write(html.getBytes());
        out.flush();
        out.close();
    }



    public void generateCode(GeneratePo po) throws Exception{
        for (String tableName : po.getTableNames()) {
            TableInfo table =   this.findTable(tableName);
            List<ColumnInfo> columns = table.getColumnList();

            for (ViewEnum viewEnum : ViewEnum.values()) {
                genFile(po,viewEnum,table);
            }
        }

    }
}
